package com.work.yshop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.model.ChatMessage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_SENT = 1;
    private static final int VIEW_TYPE_RECEIVED = 2;
    
    private List<ChatMessage> messages = new ArrayList<>();
    private String contactImageUrl;
    
    public void setData(List<ChatMessage> messages) {
        this.messages = messages;
        notifyDataSetChanged();
    }
    
    public void addMessage(ChatMessage message) {
        this.messages.add(message);
        notifyItemInserted(messages.size() - 1);
    }
    
    public void setContactImageUrl(String imageUrl) {
        this.contactImageUrl = imageUrl;
    }
    
    @Override
    public int getItemViewType(int position) {
        ChatMessage message = messages.get(position);
        return message.getType() == ChatMessage.TYPE_SENT ? VIEW_TYPE_SENT : VIEW_TYPE_RECEIVED;
    }
    
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_SENT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_sent, parent, false);
            return new SentMessageViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_received, parent, false);
            return new ReceivedMessageViewHolder(view);
        }
    }
    
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatMessage message = messages.get(position);
        
        // 处理时间显示逻辑
        boolean showTime = shouldShowTime(position);
        
        if (holder instanceof SentMessageViewHolder) {
            SentMessageViewHolder viewHolder = (SentMessageViewHolder) holder;
            viewHolder.messageView.setText(message.getContent());
            
            if (showTime) {
                viewHolder.timeView.setVisibility(View.VISIBLE);
                viewHolder.timeView.setText(message.getFormattedTime());
            } else {
                viewHolder.timeView.setVisibility(View.GONE);
            }
            
            // 在实际应用中，这里会使用Glide加载用户头像
            // Glide.with(viewHolder.itemView.getContext()).load(userAvatarUrl).into(viewHolder.avatarView);
        } else if (holder instanceof ReceivedMessageViewHolder) {
            ReceivedMessageViewHolder viewHolder = (ReceivedMessageViewHolder) holder;
            viewHolder.messageView.setText(message.getContent());
            
            if (showTime) {
                viewHolder.timeView.setVisibility(View.VISIBLE);
                viewHolder.timeView.setText(message.getFormattedTime());
            } else {
                viewHolder.timeView.setVisibility(View.GONE);
            }
            
            // 在实际应用中，这里会使用Glide加载联系人头像
            // Glide.with(viewHolder.itemView.getContext()).load(contactImageUrl).into(viewHolder.avatarView);
        }
    }
    
    /**
     * 判断是否需要显示时间
     * 规则：第一条消息显示时间，两条消息间隔超过5分钟显示时间
     */
    private boolean shouldShowTime(int position) {
        if (position == 0) {
            return true;
        }
        
        ChatMessage currentMessage = messages.get(position);
        ChatMessage previousMessage = messages.get(position - 1);
        
        long timeDiff = currentMessage.getTimestamp() - previousMessage.getTimestamp();
        return timeDiff > 5 * 60 * 1000; // 5分钟
    }
    
    @Override
    public int getItemCount() {
        return messages.size();
    }
    
    static class SentMessageViewHolder extends RecyclerView.ViewHolder {
        TextView messageView;
        TextView timeView;
        ImageView avatarView;
        
        public SentMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageView = itemView.findViewById(R.id.tv_message);
            timeView = itemView.findViewById(R.id.tv_time);
            avatarView = itemView.findViewById(R.id.iv_avatar);
        }
    }
    
    static class ReceivedMessageViewHolder extends RecyclerView.ViewHolder {
        TextView messageView;
        TextView timeView;
        ImageView avatarView;
        
        public ReceivedMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageView = itemView.findViewById(R.id.tv_message);
            timeView = itemView.findViewById(R.id.tv_time);
            avatarView = itemView.findViewById(R.id.iv_avatar);
        }
    }
} 